<template>
  <el-dialog
      v-model="deviceEditWinDialogVisible"
      title="编辑设备类型"
      width="60%"
      @close="handleClose"
      draggable
      center
  >
    <template #default>
      <el-form :model="editDevice">
        <template v-if="editDevice.deviceParent === 2">
          <el-form-item label="父类型序号">
            <el-input readonly v-model="editDevice.deviceParentId"/>
          </el-form-item>
        </template>
        <el-form-item label="设备类型名称">
          <el-input v-model="editDevice.deviceName"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="editDevice.deviceRemark"/>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="editDevice.deviceStatus">
            <el-radio :value="1" size="large">是</el-radio>
            <el-radio :value="2" size="large">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editDeviceSubmit">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";
import deviceRequest from "@/api/device.js";

const props = defineProps(['deviceId']);
const emits = defineEmits(['cancelEdit','editComplete' ])
let deviceEditWinDialogVisible = ref(false)
let editDevice = ref({})


/**
 * 取消编辑的按钮
 */
function cancel() {
  emits("cancelEdit");
  deviceEditWinDialogVisible.value = false;
}

/**
 * 关闭窗体前的回调函数
 */
function handleClose() {
  emits("cancelEdit");
}

function loadDeviceById() {
  deviceRequest.selectDeviceById(props.deviceId, res => {
    if (res.data) {
      editDevice.value = res.data;
      deviceEditWinDialogVisible.value = true;
    }
  })
}

function editDeviceSubmit() {
  deviceRequest.updateDeviceById(editDevice.value, res => {
    if (res.code == 1) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      deviceEditWinDialogVisible.value = false;
      // 触发编辑完成的事件
      emits('editComplete');
    } else {
      ElMessage({
        type: "warning",
        message: "编辑失败"
      });
    }
  })
}

watch(() => props.deviceId, () => {
  // 当props中的projectId发生变化的时候，我们就重新加载数据
  loadDeviceById();
});
</script>
<style scoped>

</style>